package com.lqzs.businessmanager.utils

import android.content.Context
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import android.widget.Toast
import com.lqzs.businessmanager.R

fun show(context: Context, resId: Int) {
    showToast(context, context.resources.getString(resId))
}

fun show(context: Context?, text: String?) {
    showToast(context, text)
}

private var mToast: Toast? = null

fun showToast(context: Context?, text: String?) {
    if (context == null) {
        return
    }
    if (TextUtils.isEmpty(text)) return
    if (mToast == null) {
        mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT)
    } else {
        mToast!!.setText(text)
        mToast!!.duration = Toast.LENGTH_SHORT
    }
    mToast!!.show()
}

fun showToast(context: Context?, text: String?, time: Int) {
    if (context == null) {
        return
    }
    if (mToast == null) {
        mToast = Toast.makeText(context, text, time)
    } else {
        mToast!!.setText(text)
        mToast!!.duration = time
    }
    mToast!!.show()
}

fun cancelToast() {
    if (mToast != null) {
        mToast!!.cancel()
        mToast = null
    }
}

fun showCustomToast(context: Context?, text: String?) {
    if (context == null) {
        return
    }
    if (TextUtils.isEmpty(text)) return
    val inflater = LayoutInflater.from(context)
    val toastView: View =
        inflater.inflate(R.layout.layout_toast, null) // 用布局解析器来解析一个布局去适配Toast的setView方法
    val tvText = toastView.findViewById<TextView>(R.id.tv_text)
    tvText.text = text
    val toast = Toast(context)
    toast.view = toastView
    toast.duration = Toast.LENGTH_SHORT
    toast.show()
}

fun showCustomToast(context: Context?, text: String?, time: Int) {
    if (context == null) {
        return
    }
    if (TextUtils.isEmpty(text)) return
    val inflater = LayoutInflater.from(context)
    val toastView: View =
        inflater.inflate(R.layout.layout_toast, null) // 用布局解析器来解析一个布局去适配Toast的setView方法
    val tvText = toastView.findViewById<TextView>(R.id.tv_text)
    tvText.text = text
    val toast = Toast(context)
    toast.setView(toastView)
    toast.duration = time
    toast.show()
}

fun showCustomCenterToast(context: Context?, text: String?) {
    if (context == null) {
        return
    }
    if (TextUtils.isEmpty(text)) return
    val inflater = LayoutInflater.from(context)
    val toastView: View =
        inflater.inflate(R.layout.layout_toast, null) // 用布局解析器来解析一个布局去适配Toast的setView方法
    val tvText = toastView.findViewById<TextView>(R.id.tv_text)
    tvText.text = text
    val toast = Toast(context)
    toast.setView(toastView)
    toast.duration = Toast.LENGTH_SHORT
    toast.setGravity(Gravity.CENTER, 0, 0)
    toast.show()
}

fun showCustomCenterToast(context: Context?, text: String?, time: Int) {
    if (context == null) {
        return
    }
    if (TextUtils.isEmpty(text)) return
    val inflater = LayoutInflater.from(context)
    val toastView: View =
        inflater.inflate(R.layout.layout_toast, null) // 用布局解析器来解析一个布局去适配Toast的setView方法
    val tvText = toastView.findViewById<TextView>(R.id.tv_text)
    tvText.text = text
    val toast = Toast(context)
    toast.setView(toastView)
    toast.duration = time
    toast.setGravity(Gravity.CENTER, 0, 0)
    toast.show()
}

fun showCustomCenterToast(context: Context?, text: String?, xOffSet: Int, yOffset: Int) {
    if (context == null) {
        return
    }
    if (TextUtils.isEmpty(text)) return
    val inflater = LayoutInflater.from(context)
    val toastView: View =
        inflater.inflate(R.layout.layout_toast, null) // 用布局解析器来解析一个布局去适配Toast的setView方法
    val tvText = toastView.findViewById<TextView>(R.id.tv_text)
    tvText.text = text
    val toast = Toast(context)
    toast.setView(toastView)
    toast.duration = Toast.LENGTH_SHORT
    toast.setGravity(Gravity.CENTER, xOffSet, yOffset)
    toast.show()
}
